package com.mmall.concurrency.example.singleton;

import com.mmall.concurrency.annoations.ThreadSafe;

/**
 * 懒汉模式 双重同步锁单例模式 双重检测机制+volatile
 * 单例实例在第一次使用时进行创建
 */

@ThreadSafe
public class SingletonExample5 {

    //私有构造函数
    private SingletonExample5()
    {

    }

    //单例对象
    private volatile static SingletonExample5 singleton = null;

    // 静态的工厂方法
    public static SingletonExample5 getSingleton() {
        if(singleton == null)
        {
            synchronized (SingletonExample5.class)
            {
                if(singleton == null)
                {
                    singleton = new SingletonExample5();
                }
            }

        }
        return singleton;
    }
}
